import java.util.List;
import java.util.Scanner;

public class Main {
    private static final Scanner scanner = new Scanner(System.in);
    private static final GradeManager manager = new GradeManager();

    public static void main(String[] args) {
        while (true) {
            displayMenu();
            int choice = getIntInput("请输入选项序号：");

            switch (choice) {
                case 1:
                    recordGrade();
                    break;
                case 2:
                    queryGrades();
                    break;
                case 3:
                    statisticsCourse();
                    break;
                case 4:
                    System.out.println("感谢使用学生成绩管理系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("错误：无效的选项序号！");
                    pressEnterToContinue();
            }
        }
    }

    private static void displayMenu() {
        clearScreen();
        System.out.println("=================================");
        System.out.println("欢迎使用学生成绩管理系统");
        System.out.println("=================================");
        System.out.println("请选择操作：");
        System.out.println("1. 记录学生成绩");
        System.out.println("2. 查询学生成绩");
        System.out.println("3. 统计课程成绩");
        System.out.println("4. 退出系统");
        System.out.println();
    }

    private static void recordGrade() {
        clearScreen();
        System.out.println("===== 记录学生成绩 =====");

        String name = getStringInput("请输入学生姓名：");
        String studentId = getStringInput("请输入学生学号：");
        String course = getStringInput("请输入课程名称：");
        double score = getDoubleInput("请输入成绩（0-100）：");

        manager.addStudent(name, studentId, course, score);
        pressEnterToContinue();
    }

    private static void queryGrades() {
        clearScreen();
        System.out.println("===== 查询学生成绩 =====");
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");

        int queryChoice = getIntInput("请输入选项序号：");
        List<Student> results = null;

        switch (queryChoice) {
            case 1:
                String name = getStringInput("请输入学生姓名：");
                results = manager.queryByName(name);
                break;
            case 2:
                String studentId = getStringInput("请输入学生学号：");
                results = manager.queryById(studentId);
                break;
            case 3:
                String course = getStringInput("请输入课程名称：");
                results = manager.queryByCourse(course);
                break;
            default:
                System.out.println("错误：无效的选项序号！");
                pressEnterToContinue();
                return;
        }

        if (results.isEmpty()) {
            System.out.println("未找到相关记录！");
        } else {
            for (Student student : results) {
                System.out.printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f%n",
                        student.getName(), student.getStudentId(),
                        student.getCourse(), student.getScore());
            }
        }

        pressEnterToContinue();
    }

    private static void statisticsCourse() {
        clearScreen();
        System.out.println("===== 统计课程成绩 =====");
        String course = getStringInput("请输入课程名称：");
        manager.statisticsByCourse(course);
        pressEnterToContinue();
    }

    // 辅助方法
    private static void clearScreen() {
        try {
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        } catch (Exception e) {
            System.out.println("\n".repeat(50)); // 如果清屏失败，打印50个空行
        }
    }

    private static String getStringInput(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine().trim();
    }

    private static int getIntInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                int input = Integer.parseInt(scanner.nextLine());
                return input;
            } catch (NumberFormatException e) {
                System.out.println("错误：请输入有效的整数！");
            }
        }
    }

    private static double getDoubleInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                double input = Double.parseDouble(scanner.nextLine());
                return input;
            } catch (NumberFormatException e) {
                System.out.println("错误：请输入有效的数字！");
            }
        }
    }

    private static void pressEnterToContinue() {
        System.out.println("\n按Enter键返回菜单...");
        scanner.nextLine();
    }
}
